﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using WF_QLThuVien.Utilities;
using System.Data;
using WF_QLThuVien.Entities.QuyDinh;
using WF_QLThuVien.Entities.Sach;
using WF_QLThuVien.Entities.PhieuMuon;
using WF_QLThuVien.Entities.DauSach;
using WF_QLThuVien.Entities;

using System.Windows.Forms;

namespace WF_QLThuVien.DataLayer
{
    class DataAccessHelper
    {
        #region: Khai báo các thành viên dữ liệu
        OleDbCommand cmd;
        OleDbConnection conn;
        #endregion

        #region: Các phương thức khởi tạo
        public DataAccessHelper()
        {
            conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + CVariable.PATH);
        }
        #endregion

        #region: Các phương thức thao tác với CSDL
        private void Open()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
        }

        private void Close()
        {
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        
        public object GetValue(string query)
        {
            try
            {
                Open();
                cmd = new OleDbCommand(query, conn);

                return cmd.ExecuteScalar();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi DatAccessHelper.GetValue\n" + ex.ToString());
            }
        }


       

        public CQuyDinh GetQuyDinh(string query)
        {
            try
            {
                Open();
                CQuyDinh qd = new CQuyDinh();
                cmd = new OleDbCommand(query, conn);
                OleDbDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    qd.PhiLamThe = double.Parse(rd["PhiLamThe"].ToString());
                    qd.SoLanGiaHan = int.Parse(rd["SoLanGiaHan"].ToString());
                    qd.SoNgayMotLanGiaHan = int.Parse(rd["SoNgayMotLanGiaHan"].ToString());
                    qd.SoNgayMuonToiDa = int.Parse(rd["SoNgayMuonToiDa"].ToString());
                    qd.SoSachMuonToiDa = int.Parse(rd["SoSachMuonToiDa"].ToString());
                    qd.TienKyQuyToiThieu = double.Parse(rd["TienKyQuyToiThieu"].ToString());
                    qd.TienPhatMatSach = int.Parse(rd["TienPhatMatSach"].ToString());                    
                    qd.TienPhatTreHan = double.Parse(rd["TienPhatTreHan"].ToString());
                }
                return qd;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi DatAccessHelper.GetQuyDinh\n" + ex.ToString());
            }
        }

        public CCTPhieuMuon GetCTPhieuMuon(string query)
        {
            try
            {
                Open();
                CCTPhieuMuon ctpm = new CCTPhieuMuon();
                cmd = new OleDbCommand(query, conn);
                OleDbDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    ctpm.NgayTra = Convert.ToDateTime(rd["NgayTra"]);
                    ctpm.PhieuMuonID = rd["PhieuMuonID"].ToString();

                }
                return ctpm;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi DatAccessHelper.GetCTPhieuMuon \n" + ex.ToString());
            }
        }      

        public DataTable GetDataTable(string select)
        {
            try
            {
                OleDbDataAdapter da = new OleDbDataAdapter(select, conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi DatAccessHelper.GetDataTable \n" + ex.ToString());
            }
        }

        public int ExecuteNonQuery(string query)
        {
            try
            {
                Open();
                cmd = new OleDbCommand(query, conn);
                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi DatAccessHelper.ExecuteNonQuery \n" + ex.ToString());
            }
        }

        #endregion





    }
}
